package set_;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author DCSGO
 * @version 1.0
 * 定义一个Employee类，该类包含:
 * private成员属性name,sal,birthday(MyDate类型)
 * 其中 birthday 为 MyDate类型(属性包括: year, month, day)
 * 要求:
 * 1.创建3个Employee放入HashSet中
 * 2当name和birthday的值相同时，认为是相同员工,不能添加到HashSet集合中
 */

@SuppressWarnings({"all"})
public class HashSetExercise02 {
    //因为要求当 name 和 birthday 的值相同时不添加
    //所以不光要重写 Employee02 的 hashCode 和 equals
    //还得重写 MyDate 的 HashCode 和 equals
    //不重写 MyDate 的 HashCode 和 equals 那
    //Employee02 中重写的 hashCode 和 equals 也将没有意义
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Employee02("小王", 1999.1, new MyDate(2000, 10, 1)));
        hashSet.add(new Employee02("老张", 3999.3, new MyDate(1998, 5, 5)));
        hashSet.add(new Employee02("佳慧", 2999.1, new MyDate(1999, 10, 18)));
        hashSet.add(new Employee02("老张", 3999.3, new MyDate(1998, 5, 5)));
        hashSet.add(new Employee02("佳慧", 6666, new MyDate(1956, 7, 1)));
        hashSet.add(new Employee02("佳慧", 50000, new MyDate(1999, 10, 18)));
        System.out.println(hashSet);

        /**
         * [
         * Employee02{name='佳慧', sal=2999.1, birthday=1999-10-18},
         * Employee02{name='老张', sal=3999.3, birthday=1998-5-5},
         * Employee02{name='佳慧', sal=6666.0, birthday=1956-7-1},
         * Employee02{name='小王', sal=1999.1, birthday=2000-10-1}]
         */
    }
}

class Employee02 {
    private String name;
    private double sal;
    private MyDate birthday;

    @Override
    public String toString() {
        return "\nEmployee02{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee02 that = (Employee02) o;
        return Objects.equals(name, that.name) && Objects.equals(birthday, that.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public Employee02(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }
}

class MyDate {
    int year;
    int month;
    int day;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyDate myDate = (MyDate) o;
        return year == myDate.year && month == myDate.month && day == myDate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }

    @Override
    public String toString() {
        return year + "-" + month + "-" + day;
    }

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
}